package com.znsd.zl.contract.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.znsd.zl.contract.dao.IContractDao;
import com.znsd.zl.contract.dao.ContractTemplateDao;
import com.znsd.zl.contract.entity.*;
import com.znsd.zl.contract.service.IContractService;
import com.znsd.zl.contract.utils.ContractTemplateQueryUtils;
import com.znsd.zl.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName: ContractServiceImpl
 * @Program: zlhouse-manage
 * @Description: 合同业务层接口实现
 * @Author: tangjun
 * @Date: 2020-08-19 11:47
 * @Version 1.0
 **/
@Service("contractService")
public class ContractServiceImpl extends ServiceImpl<IContractDao, ContractEntity> implements IContractService {
	
	@Resource
	private IContractDao iContractDao;
	
	@Resource
	private ContractTemplateDao contractTemplateDao;
	
	@Resource
	ContractTemplateQueryUtils contractTemplateQueryUtils;
	
	/**
	 * 条件查询
	 * @param id 查询条件
	 * @return 返回 ContractEntity 对象
	 */
	@Override
	public HouseEntity queryHousesery(Integer id) {
		return iContractDao.queryHousesery(id);
	}
	
	/**
	 * 条件查询
	 * @param id 查询条件
	 * @return 返回 ContractEntity 对象
	 */
	@Override
	public UserEntity queryUser(Integer id) {
		return iContractDao.queryUser(id);
	}
	
	/**
	 * 查询全部
	 * @return 返回 ContractQueryEntity 集合对象
	 */
	@Override
	public PageUtils queryContract(Integer pageNum, Integer pageSize, Map<String, Object> params) {
		Page<ContractQueryEntity> page=new Page<>(pageNum,pageSize);
		IPage<ContractQueryEntity>  allOrder =  baseMapper.queryContract(page, params);
		List<ContractQueryEntity> records=new ArrayList<>();
		Integer c=pageNum==1?0:pageNum*pageSize-pageSize;
		for(int i=c;i<pageNum*pageSize;i++){
			if (i==allOrder.getRecords().size()){
				break;
			}
			records.add(allOrder.getRecords().get(i));
			
		}
		PageUtils pageUtils = new PageUtils(allOrder);
		pageUtils.setList(records);
		Integer totalCount=allOrder.getRecords().size();
		pageUtils.setTotalCount(totalCount);
		pageUtils.setTotalPage((totalCount+pageSize-1)/pageSize);
		return pageUtils;
	}
	
	
	/**
	 * @Author tangjun
	 * @Description 根据contractAdminEntity添加数据
	 * @Date  2020/8/23 0023
	 * @param contractAdminEntity 指定的条件
	 * @return 返回 Integer 状态
	 **/
	@Override
	public Integer addContractAdmin(ContractQueryEntity contractAdminEntity) {
		//封装数据
		ContractEntity contractEntity=new ContractEntity();
		//获取T_RENT表的所有数据
		RentEntity rent=iContractDao.queryRentName(contractAdminEntity.getTitle());
		if (rent == null){
			return 2;
		}
		contractEntity.setHouseId(rent.getHouseId());
		//根据T_RENT获取房源id
		HouseEntity houseEntity= iContractDao.queryHousesery(rent.getHouseId());
		if (houseEntity == null) {
			return 2;
		}
		//获取根据用户名称查询用户id
		UserEntity userEntity= iContractDao.queryUserName(contractAdminEntity.getUserName());
		if (userEntity == null){
			return 2;
		}
		//获取用户id
		contractEntity.setUserId(userEntity.getUserId());
		//根据合同id获取合同路径
		ContractTemplateEntity contractTemplateEntity = contractTemplateDao.queryTempl(Integer.valueOf(contractAdminEntity.getTemplate()));
		//获取到的合同路径
		contractEntity.setTemplate(contractTemplateEntity.getTemplateSite());
		//根据房东名称查询房东id
		UserEntity entitye= iContractDao.queryUserName(contractAdminEntity.getLandlordName());
		//获取到的房东id
		contractEntity.setLandlordId(entitye.getUserId());
		//合同开始时间
		contractEntity.setBeginDate(contractAdminEntity.getBeginDate());
		//合同开始时间
		contractEntity.setEndDate(contractAdminEntity.getEndDate());
		//设置日期格式
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//获取当前时间
		contractEntity.setExactTime(df.format(new Date()));
		//获取合同名称
		contractEntity.setContractName(contractAdminEntity.getContractName());
		//获取合同状态
		contractEntity.setContractStatus(contractAdminEntity.getContractStatus());
		System.out.println(contractEntity);
		
		
		//将获取的数据插入合同内 获取反回的路径
		String url= contractTemplateQueryUtils.contractTemplaterAdmin(houseEntity,userEntity,entitye,contractEntity);
			//添加已赋值的合同路径
		if (url==null){
			url="https://me-file.oss-cn-shenzhen.aliyuncs.com/upload/20200829/19d6578a5e234080bf580c21e8b88901.pdf";
		}
			contractEntity.setTemplate(url);
		System.out.println("====="+contractEntity);
			save(contractEntity);
			System.out.println("数据添加成功");
		return 1;
	}
	
	/**
	 * @Author tangjun
	 * @description app 进行合同添加
	 * @date  2020/8/29 0029
	 * @param contractEntity 数据填充
	 * @return java.lang.Integer
	 **/
	@Override
	public Integer addContract(ContractEntity contractEntity) {
		
		//获取合同地址表的数据
		ContractTemplateEntity tc= contractTemplateDao.queryTempl(Integer.valueOf(contractEntity.getTemplate()));
		if (tc==null){
			return 2;
		}
		//获取房源表数据
		HouseEntity houseEntity=iContractDao.queryHousesery(contractEntity.getHouseId());
		if (houseEntity==null){
			return 2;
		}
		//获取房东表数据
		UserEntity land=iContractDao.queryUser(contractEntity.getLandlordId());
		if (land==null){
			return 2;
		}
		//获取用户表数据
		UserEntity userEntity=iContractDao.queryUser(contractEntity.getUserId());
		if (userEntity==null){
			return 2;
		}
		//设置日期格式
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//获取当前时间
		contractEntity.setExactTime(df.format(new Date()));
		
		String url= contractTemplateQueryUtils.contractTemplaterQuery(userEntity,houseEntity,land,tc,contractEntity);
	
			//给一个默认的合同名称
		if (url==null){
			url="https://me-file.oss-cn-shenzhen.aliyuncs.com/upload/20200829/19d6578a5e234080bf580c21e8b88901.pdf";
		}
		if (contractEntity.getContractName() == null){
			contractEntity.setContractName("默认合同名称");
		}
		contractEntity.setTemplate(url);
		save(contractEntity);
		System.out.println("数据添加成功");
		return 1;
	}
	
	
	/**
	 * @Author tangjun
	 * @Description 根据id查询数据
	 * @Date  2020/8/23 0023
	 * @param id 指定的条件
	 * @return 返回 ContractQueryEntity 对象进行存储
	 **/
	@Override
	public ContractQueryEntity qeryAdmin(Integer id) {
		return iContractDao.qeryAdmin(id);
	}
	
	/**
	 * @Author tangjun
	 * @Description 根据contractEntity查询数据
	 * @Date  2020/8/23 0023
	 * @param contractEntity 指定的条件
	 * @return 返回  List<ContractEntity> 对象进行存储
	 **/
	@Override
	public List<ContractQueryEntity> getAdminQuery(ContractEntity contractEntity) {
		return iContractDao.getAdminQuery(contractEntity);
	}
	
	
	/**
	 * @Author tangjun
	 * @Description 根据contractQueryEntity修改数据
	 * @Date  2020/8/23 0023
	 * @param contractQueryEntity 指定的条件
	 * @return 返回 Integer 状态
	 **/
	@Override
	public Integer updateAdmin(ContractQueryEntity contractQueryEntity) {
		ContractEntity contractEntity=new ContractEntity();
		
		//获取T_RENT表的所有数据
		RentEntity rent=iContractDao.queryRentName(contractQueryEntity.getTitle());
		if (rent == null){
			return 2;
		}
		//传入房源id
		contractEntity.setHouseId(rent.getHouseId());
		//获取指定房源数据
		HouseEntity houseEntity= iContractDao.queryHousesery(rent.getHouseId());
		if (houseEntity == null) {
			return 2;
		}
		//获取根据用户名称查询用户id
		UserEntity userEntity= iContractDao.queryUserName(contractQueryEntity.getUserName());
		if (userEntity == null){
			return 2;
		}
		//传入用户id
		contractEntity.setUserId(userEntity.getUserId());
		//根据房东名称查询房东id
		UserEntity entitye= iContractDao.queryUserName(contractQueryEntity.getLandlordName());
		//传入房东id
		contractEntity.setLandlordId(entitye.getUserId());
		//设置日期格式
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//获取当前时间
		contractEntity.setExactTime(df.format(new Date()));
		//获取id
		contractEntity.setId(contractQueryEntity.getId());
		//获取合同名称
		contractEntity.setContractName(contractQueryEntity.getContractName());
		//获取开始时间
		contractEntity.setBeginDate(contractQueryEntity.getBeginDate());
		//获取结束时间
		contractEntity.setEndDate(contractQueryEntity.getEndDate());
		//获取合同状态
		contractEntity.setContractStatus(contractQueryEntity.getContractStatus());
		//根据合同id获取合同路径
		ContractTemplateEntity contractTemplateEntity = contractTemplateDao.queryTempl(Integer.valueOf(contractQueryEntity.getTemplate()));
		contractEntity.setTemplate(contractTemplateEntity.getTemplateSite());
		contractQueryEntity.setTemplate(contractTemplateEntity.getTemplateSite());
		//进行数据的传入
		String url=	contractTemplateQueryUtils.contractTemplaterUpdateAdmin(houseEntity,userEntity,entitye,contractQueryEntity);
		//获取上传得到的路径
		if (url==null){
			url="https://me-file.oss-cn-shenzhen.aliyuncs.com/upload/20200829/19d6578a5e234080bf580c21e8b88901.pdf";
		}
		contractEntity.setTemplate(url);
		iContractDao.updateAdmin(contractEntity);
		return 1;
	}
	
	
}
